import request from '@/utils/request'

// ==================== 认证相关API ====================

/**
 * 登录
 */
export function login(data: {
  username: string
  password: string
  code: string
  uuid: string
}) {
  return request({
    url: '/auth/login',
    method: 'POST',
    data
  })
}

/**
 * 注册
 */
export function register(data: {
  username: string
  password: string
  phone: string
  code: string
}) {
  return request({
    url: '/auth/register',
    method: 'POST',
    data
  })
}

/**
 * 获取验证码
 */
export function getCaptcha() {
  return request({
    url: '/auth/code',
    method: 'GET'
  })
}

/**
 * 发送短信验证码
 */
export function sendSmsCode(phone: string) {
  return request({
    url: '/resource/sms/code',
    method: 'GET',
    params: {
      phonenumber: phone
    }
  })
}

/**
 * 重置密码
 */
export function resetPassword(data: {
  phone: string
  code: string
  newPassword: string
}) {
  return request({
    url: '/auth/reset-password',
    method: 'POST',
    data
  })
}

/**
 * 获取用户信息
 */
export function getUserInfo() {
  return request({
    url: '/system/user/getInfo',
    method: 'GET'
  })
}

/**
 * 登出
 */
export function logout() {
  return request({
    url: '/auth/logout',
    method: 'POST'
  })
}

// ==================== 用户相关API ====================

/**
 * 上传头像
 */
export function uploadAvatar(file: File) {
  const formData = new FormData()
  formData.append('avatarfile', file)
  
  return request({
    url: '/system/user/profile/avatar',
    method: 'POST',
    data: formData,
    header: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 更新用户头像
 */
export function updateUserAvatar(avatarUrl: string) {
  return request({
    url: '/system/user/profile',
    method: 'PUT',
    data: {
      avatar: avatarUrl
    }
  })
}

/**
 * 更新用户资料
 */
export function updateUserProfile(data: any) {
  return request({
    url: '/system/user/profile',
    method: 'PUT',
    data
  })
}

// ==================== 需求相关API ====================

/**
 * 发布需求
 */
export function publishDemand(data: {
  demandName: string
  demandCategory: string
  demandLocation: string
  demandBudget: string
  demandDescription: string
  demandContactPerson: string
  demandContactPhone: string
  demandDeadline?: string
}) {
  return request({
    url: '/system/demands',
    method: 'POST',
    data: {
      title: data.demandName,
      category: data.demandCategory,
      location: data.demandLocation,
      budget: data.demandBudget,
      description: data.demandDescription,
      contactPerson: data.demandContactPerson,
      contactPhone: data.demandContactPhone,
      deadline: data.demandDeadline
    }
  })
}

/**
 * 获取需求列表
 */
export function getDemandList(params?: {
  pageNum?: number
  pageSize?: number
  category?: string
  location?: string
  keyword?: string
}) {
  return request({
    url: '/system/demands',
    method: 'GET',
    params: {
      pageNum: params?.pageNum || 1,
      pageSize: params?.pageSize || 10,
      category: params?.category,
      location: params?.location,
      keyword: params?.keyword
    }
  })
}

/**
 * 获取需求详情
 */
export function getDemandDetail(demandId: string | number) {
  return request({
    url: `/system/demands/${demandId}`,
    method: 'GET'
  })
}

/**
 * 更新需求
 */
export function updateDemand(demandId: string | number, data: any) {
  return request({
    url: `/system/demands/${demandId}`,
    method: 'PUT',
    data
  })
}

/**
 * 删除需求
 */
export function deleteDemand(demandId: string | number) {
  return request({
    url: `/system/demands/${demandId}`,
    method: 'DELETE'
  })
}

// ==================== 服务相关API ====================

/**
 * 发布服务
 */
export function publishService(data: {
  serviceName: string
  serviceCategory: string
  serviceLocation: string
  servicePrice: string
  serviceDescription: string
  serviceContactPerson: string
  serviceContactPhone: string
  serviceImages?: string[]
}) {
  return request({
    url: '/system/services',
    method: 'POST',
    data: {
      title: data.serviceName,
      category: data.serviceCategory,
      location: data.serviceLocation,
      price: data.servicePrice,
      description: data.serviceDescription,
      contactPerson: data.serviceContactPerson,
      contactPhone: data.serviceContactPhone,
      images: data.serviceImages
    }
  })
}

/**
 * 获取服务列表
 */
export function getServiceList(params?: {
  pageNum?: number
  pageSize?: number
  category?: string
  location?: string
  keyword?: string
}) {
  return request({
    url: '/system/services',
    method: 'GET',
    params: {
      pageNum: params?.pageNum || 1,
      pageSize: params?.pageSize || 10,
      category: params?.category,
      location: params?.location,
      keyword: params?.keyword
    }
  })
}

/**
 * 获取服务详情
 */
export function getServiceDetail(serviceId: string | number) {
  return request({
    url: `/system/services/${serviceId}`,
    method: 'GET'
  })
}

/**
 * 更新服务
 */
export function updateService(serviceId: string | number, data: any) {
  return request({
    url: `/system/services/${serviceId}`,
    method: 'PUT',
    data
  })
}

/**
 * 删除服务
 */
export function deleteService(serviceId: string | number) {
  return request({
    url: `/system/services/${serviceId}`,
    method: 'DELETE'
  })
}

// ==================== 商户相关API ====================

/**
 * 判断是否为商户
 */
export function checkIsMerchant() {
  return request({
    url: '/business/merchant/isMerchant',
    method: 'GET'
  })
}

/**
 * 新增商户信息
 */
export function addMerchantInfo(data: {
  merchantCategory: string
  merchantDescription: string
  merchantLocation?: string
}) {
  return request({
    url: '/business/merchant',
    method: 'POST',
    data
  })
}

/**
 * 获取我的商户信息
 */
export function getMyMerchantInfo() {
  return request({
    url: '/business/merchant/myMerchantInfo',
    method: 'GET'
  })
}

// ==================== 字典相关API ====================

/**
 * 根据字典类型查询字典数据
 */
export function getDictDataByType(dictType: string) {
  return request({
    url: `/system/dict/data/type/${dictType}`,
    method: 'GET'
  })
}

// ==================== 收藏相关API ====================

/**
 * 收藏服务
 */
export function favoriteService(serviceId: string | number) {
  return request({
    url: '/system/favorites/service',
    method: 'POST',
    data: {
      serviceId
    }
  })
}

/**
 * 取消收藏服务
 */
export function unfavoriteService(serviceId: string | number) {
  return request({
    url: `/system/favorites/service/${serviceId}`,
    method: 'DELETE'
  })
}

/**
 * 收藏需求
 */
export function favoriteDemand(demandId: string | number) {
  return request({
    url: '/system/favorites/demand',
    method: 'POST',
    data: {
      demandId
    }
  })
}

/**
 * 取消收藏需求
 */
export function unfavoriteDemand(demandId: string | number) {
  return request({
    url: `/system/favorites/demand/${demandId}`,
    method: 'DELETE'
  })
}

/**
 * 获取我的收藏列表
 */
export function getMyFavorites(type?: 'service' | 'demand') {
  return request({
    url: '/system/favorites',
    method: 'GET',
    params: {
      type
    }
  })
}

// ==================== 消息相关API ====================

/**
 * 获取消息列表
 */
export function getMessageList(params?: {
  pageNum?: number
  pageSize?: number
  type?: string
}) {
  return request({
    url: '/system/messages',
    method: 'GET',
    params: {
      pageNum: params?.pageNum || 1,
      pageSize: params?.pageSize || 10,
      type: params?.type
    }
  })
}

/**
 * 获取消息详情
 */
export function getMessageDetail(messageId: string | number) {
  return request({
    url: `/system/messages/${messageId}`,
    method: 'GET'
  })
}

/**
 * 标记消息为已读
 */
export function markMessageAsRead(messageId: string | number) {
  return request({
    url: `/system/messages/${messageId}/read`,
    method: 'PUT'
  })
}

/**
 * 删除消息
 */
export function deleteMessage(messageId: string | number) {
  return request({
    url: `/system/messages/${messageId}`,
    method: 'DELETE'
  })
}

// ==================== 公告相关API ====================

/**
 * 获取公告列表
 */
export function getAnnouncementList(params?: {
  pageNum?: number
  pageSize?: number
  category?: string
}) {
  return request({
    url: '/system/announcements',
    method: 'GET',
    params: {
      pageNum: params?.pageNum || 1,
      pageSize: params?.pageSize || 10,
      category: params?.category
    }
  })
}

/**
 * 获取公告详情
 */
export function getAnnouncementDetail(announcementId: string | number) {
  return request({
    url: `/system/announcements/${announcementId}`,
    method: 'GET'
  })
}

// ==================== 文件上传相关API ====================

/**
 * 通用文件上传
 */
export function uploadFile(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/common/upload',
    method: 'POST',
    data: formData,
    header: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 批量文件上传
 */
export function uploadFiles(files: File[]) {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  
  return request({
    url: '/common/uploads',
    method: 'POST',
    data: formData,
    header: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ==================== 统计相关API ====================

/**
 * 获取用户统计数据
 */
export function getUserStats() {
  return request({
    url: '/system/user/stats',
    method: 'GET'
  })
}

/**
 * 获取平台统计数据
 */
export function getPlatformStats() {
  return request({
    url: '/system/platform/stats',
    method: 'GET'
  })
}

